package com.sunny.ynhz.ui.index;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.NewsStaticAdapter;
import com.sunny.ynhz.api.HttpManager;
import com.sunny.ynhz.api.subscriber.ApiSubscriber;
import com.sunny.ynhz.bean.NewsBean;
import com.sunny.ynhz.ui.NewsDetailActivity;
import com.sunny.ynhz.ui.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by huhuan123 on 2018/1/10.
 */

public class HotRedFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener, BaseQuickAdapter.RequestLoadMoreListener {
    List<NewsBean.DataBean.ContentBean> newsData = new ArrayList<>();
    private NewsStaticAdapter newsStaticAdapter;
    private RecyclerView refreshableView;
    private SwipeRefreshLayout swipeRefreshLayout_ly;
    int current = 1;
    private int id;
    private String title;
    @Override
    protected int setLayoutResourceID() {
        return R.layout.news_static_activity;
    }
    @Override
    protected void setUpView() {
        super.setUpView();
        Bundle arguments = getArguments();
        id = arguments.getInt("id");
        title = arguments.getString("title");
        refreshableView = mContentView.findViewById(R.id.rl_news_static);
        swipeRefreshLayout_ly = mContentView.findViewById(R.id.id_swipe_ly);
        swipeRefreshLayout_ly.setOnRefreshListener(this);
        refreshableView.setLayoutManager(new LinearLayoutManager(activity));
        newsStaticAdapter = new NewsStaticAdapter(newsData, this.id, getContext());
        refreshableView.setAdapter(newsStaticAdapter);
        newsStaticAdapter.setOnLoadMoreListener(this);
        new Handler().postDelayed(() -> {
            swipeRefreshLayout_ly.setRefreshing(true);
            getNewMsg(1);

        }, 500);
        newsStaticAdapter.setOnItemClickListener((adapter, view, position) -> {
            NewsBean.DataBean.ContentBean contentBean = newsData.get(position);
            Intent intent = new Intent(activity, NewsDetailActivity.class);
            intent.putExtra("id", contentBean.getId());
            intent.putExtra("title", title);
            startActivity(intent);
        });
    }

    public void getNewMsg(final int mCurrent) {
        HttpManager.getInstance().getNewsMsg(id, mCurrent, 10).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new ApiSubscriber(activity, new ApiSubscriber.HttpOnResultListener<NewsBean>() {
            @Override
            public void onNext(NewsBean newsBean) {
                if (mCurrent == 1) {
                    newsData.clear();
                    newsStaticAdapter.notifyDataSetChanged();
                }
                newsData.addAll(newsBean.getData().getContent());
                if (newsData.size() < newsBean.getData().getRecordTotal()) {
                    newsStaticAdapter.setEnableLoadMore(true);
                    current++;
                } else {
                    newsStaticAdapter.setEnableLoadMore(false);
                }
                newsStaticAdapter.notifyDataSetChanged();


            }

            @Override
            public void onCompleted() {
                swipeRefreshLayout_ly.setRefreshing(false);
                newsStaticAdapter.loadMoreComplete();

            }

            @Override
            public void onstart() {

            }
        }));
    }

    @Override
    public void onRefresh() {
        current = 1;
        getNewMsg(current);
    }

    @Override
    public void onLoadMoreRequested() {
        getNewMsg(current);
    }
}
